#include "pageaux.h"
#include "ui_pageaux.h"

PageAux::PageAux(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageAux)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);
    m_lAuxStatNameList  <<"R、S相间电压(V)"
                        <<"R、T相间电压(V)"
                        <<"充电机输出电流(A)"
                        <<"蓄电池充放电电流(A)"
                        <<"并网供电接触器KMK"
                        <<"辅逆输入电压(V)"
                        <<"辅逆输入电流(A)"
                        <<"三相输出接触器"
                        <<"蓄电池温度(℃)"
                        <<"充电机输出电压(V)";

    m_lAuxStatCntOfOneCar <<1
                          <<1
                          <<1
                          <<1
                          <<1
                          <<1
                          <<1
                          <<1
                          <<1
                          <<1;
    InitAuxStatLabels();
    setDefaultStat();
}

PageAux::~PageAux()
{
    delete ui;
}

void PageAux::setDefaultStat()
{
    for(int i=0;i<3;i++)
    {
        setAuxStat(i,0,0);
        setAuxStat(i,2,0);
        setAuxStat(i,3,0);
        setAuxStat(i,5,0);
    }
    setAuxStat(4,2,0);
    setAuxStat(3,0,0);
    setAuxStat(3,5,0);
    setAuxStat(5,0,0);
    setAuxStat(5,2,0);
    setAuxStat(5,3,0);
    setAuxStat(5,5,0);
    setAuxStat(6,0,0);
    setAuxStat(6,2,0);
    setAuxStat(6,3,0);
    setAuxStat(6,5,0);
    setAuxStat(7,0,0);
    setAuxStat(7,2,0);
    setAuxStat(7,3,0);
    setAuxStat(7,5,0);
    setAuxStat(8,0,0);
    setAuxStat(8,5,0);
    setAuxStat(9,0,0);
    setAuxStat(9,2,0);
    setAuxStat(9,3,0);
    setAuxStat(9,5,0);
}

void PageAux::InitAuxStatLabels()
{
    m_nStatLabelWidth_Car = MiniCarSaloonWidth+MiniCarHSpace_1220;
    m_nStatLabelSpaceX = MiniCarLeftSpace_1220 - MiniCarHSpace_1220/2;
    m_nStatLabelHeight = 55;
    m_nStatLabelStartY = 15;
    m_nStatLabelSpaceY = 0;

    uint8_t statCnt = m_lAuxStatNameList.length();
    for(int i=0;i<statCnt;i++)
    {
        QLabel *statName = new QLabel(this);
        statName->setStyleSheet(StatLabelStyle);
        statName->setAlignment(Qt::AlignCenter);
        statName->setText(m_lAuxStatNameList.at(i));
        statName->setGeometry(m_nStatLabelSpaceX-190,
                              m_nStatLabelStartY+i*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                              190,m_nStatLabelHeight);

        uint8_t statOneCarCnt = m_lAuxStatCntOfOneCar.at(i);
        QList<QLabel*> OneStatList;
        OneStatList.clear();
        for(int j=0;j<SaloonCnt;j++)
        {
            for(int k=0;k<statOneCarCnt;k++)
            {
                QLabel *stat = new QLabel(this);
                stat->setStyleSheet(StatLabelStyle);
                stat->setAlignment(Qt::AlignCenter);
                SetPosition(stat,CAR(j),ROW(i),COL(k),TOTAL(statOneCarCnt),j>1);
                OneStatList.append(stat);
            }
        }
        m_lAuxStatList.append(OneStatList);
    }
}

//row 和 col 都从0开始
void PageAux::SetPosition(QWidget* w,int car, uint8_t row, uint8_t col, uint8_t total, bool xReverse)
{
    uint OnestatLabelWidth = m_nStatLabelWidth_Car/total;
    float rowSpaceX = (m_nStatLabelWidth_Car - total*OnestatLabelWidth)/(total+1);
    if(!xReverse)
        w->setGeometry(m_nStatLabelSpaceX+rowSpaceX+car*m_nStatLabelWidth_Car+col*(OnestatLabelWidth+rowSpaceX),
                       m_nStatLabelStartY+row*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                       OnestatLabelWidth,m_nStatLabelHeight);
    else
        w->setGeometry(m_nStatLabelSpaceX+rowSpaceX+car*m_nStatLabelWidth_Car+(total-1-col)*(OnestatLabelWidth+rowSpaceX),
                       m_nStatLabelStartY+row*(m_nStatLabelHeight+m_nStatLabelSpaceY),
                       OnestatLabelWidth,m_nStatLabelHeight);
};

void PageAux::setAuxStat(uint8_t item,uint8_t index,uint16_t value)
{
    if(index >= m_lAuxStatList[item].length())
        return;

    if(item == 0 || item == 1)
    {
        if(value == 0x7FFF)
            m_lAuxStatList[item][index]->setText(QString("?"));
        else
            m_lAuxStatList[item][index]->setText(QString("%1").arg(value));
    }
    if(item == 2 || item == 3)
    {
        int16_t val = value;
        if(value == 0x7FFF)
            m_lAuxStatList[item][index]->setText(QString("?"));
        else
            m_lAuxStatList[item][index]->setText(QString("%1").arg(val));
    }
    else if(item == 4)
    {
        if(value == 0)
            m_lAuxStatList[item][index]->setText(QString("?"));
        else if(value == 1)
            m_lAuxStatList[item][index]->setText(QString("闭合"));
        else if(value == 2)
            m_lAuxStatList[item][index]->setText(QString("断开"));
    }
    if(item == 5 || item == 6)
    {
        if(value == 0xFFFF)
            m_lAuxStatList[item][index]->setText(QString("?"));
        else
            m_lAuxStatList[item][index]->setText(QString("%1").arg(value));
    }
    else if(item == 7)
    {
        if(value == 0)
            m_lAuxStatList[item][index]->setText(QString("?"));
        else if(value == 1)
            m_lAuxStatList[item][index]->setText(QString("闭合"));
        else if(value == 2)
            m_lAuxStatList[item][index]->setText(QString("断开"));
    }
    else if(item == 8)
    {
        if(value == 0x7F)
            m_lAuxStatList[item][index]->setText(QString("?"));
        else
            m_lAuxStatList[item][index]->setText(QString("%1").arg(value));
    }
    else if(item == 9)
    {
        if(value == 0xFF)
            m_lAuxStatList[item][index]->setText(QString("?"));
        else
            m_lAuxStatList[item][index]->setText(QString("%1").arg(value));
    }
}
